<script>
import {
  version,
  getSeo,
  getSiteByDomainPrefix,
  announcement
} from '@/api/http/home'
import { setSeo } from '@/util/seo'
import { versionNumber } from '@/config'
import { Base64 } from 'js-base64'
export default {
  // 首次打开APP
  onLaunch(opt) {
    const _this = this
    this.setDefaultLanguage()
    // 检查网络连接
    uni.getNetworkType({
      success: (res) => {
        if (res.networkType == 'none') {
          uni.showModal({
            title: '',
            content: this.$t('locales.noNetwork'),
            showCancel: false,
            confirmText: this.$t('locales.toSetUpEet'),
            success: function (res) {
              if (res.confirm) {
                _this.openSystemSettings()
              }
            }
          })
        }
      }
    })
    // this.setSite(opt)
    // this.$store.commit('SET_DOMAIN')
    // this.$store.dispatch('Currency', {
    //   domainPrefix: this.$store.getters.domainPrefix || ''
    // })
    // this.getMsg()
    // #ifdef H5
    this.seo()
    // #endif
  },
  onShow() {
    // 只有app才会检查更新
    // #ifdef APP-PLUS
    this.getVersion()
    // #endif
  },
  onHide() {},
  methods: {
    // 设置默认语言
    setDefaultLanguage() {
      const languageList = Object.keys(this._i18n.messages)
      if (this.$storage.get('language')) {
        languageList.map((key) => {
          if (new RegExp(key, 'i').test(this.$storage.get('language'))) {
            this._i18n.locale = key
          }
        })
      } else {
        languageList.map((key) => {
          if (new RegExp(key, 'i').test(uni.getSystemInfoSync().language)) {
            this.$storage.set(key)
            this._i18n.locale = key
          }
        })
      }

      console.log(uni.getSystemInfoSync().language)
      const tabLocales = this.$t('locales')
      uni.setTabBarItem({
        index: 0,
        text: tabLocales['pages/index/indexHome/index']
      })
      uni.setTabBarItem({
        index: 1,
        text: tabLocales['pages/sort/index']
      })
      uni.setTabBarItem({
        index: 2,
        text: tabLocales['pages/shoppingCart/index']
      })
      uni.setTabBarItem({
        index: 3,
        text: tabLocales['pages/me/index']
      })
    },
    // 获取版本信息
    getVersion() {
      version().then((res) => {
        if (res.code === 200) {
          const data = res.data
          if (data.versionCode !== versionNumber) {
            uni.reLaunch({
              url: '/pages/index/fullScreen/index?data=' + JSON.stringify(data)
            })
          }
        } else {
          uni.showToast({
            title: res.message,
            icon: 'none'
          })
        }
      })
    },

    async setSite(opt) {
      // #ifdef H5
      this.$store.commit('SET_DOMAIN')
      const isMain = await getSiteByDomainPrefix({
        domainPrefix: this.$store.getters.domainPrefix
      }).then((res) => {
        if (res.code === 200) {
          if (res.data) {
            if (res.data.type === 1) {
              this.$storage.set('isMain', true)
              return true
            } else {
              this.$storage.set('isMain', false)
              return false
            }
          } else {
            this.$storage.set('isMain', false)
            return false
          }
        }
      })
      // 是分享出来的不跳选择国家页面
      if (opt.query.share) return
      if (!isMain) return
      // #endif
      // 获取定位
      await this.$store
        .dispatch('GetLocation')
        .then((res) => {
          const nation = res.nation || ''
          if (this.$storage.get('loca')) {
            // 本地有缓存站点信息
            if (this.$storage.get('loca') !== nation && nation !== '') {
              // 位置发生变化
              uni.reLaunch({
                url: '/pages/index/home/setSites?type=1&city=' + nation
              })
            }
          } else {
            // 本地没有缓存站点信息
            uni.reLaunch({
              url: '/pages/index/home/setSites?type=0&city=' + nation
            })
          }
        })
        .catch((err) => {
          if (!this.$storage.get('loca')) {
            uni.reLaunch({
              url: '/pages/index/home/setSites?type=3'
            })
          }
        })
    },

    seo() {
      getSeo({
        domainPrefix: this.$store.getters.domainPrefix || ''
      }).then((res) => {
        if (res.code === 200) {
          const data = res.data
          if (data) {
            setSeo(
              data.description,
              data.keyWords,
              data.verificationFile,
              data.title,
              data.gaCode
            )
          }
        }
      })
    },

    getMsg() {
      announcement({
        // #ifdef H5
        terminal: 'h5',
        // #endif
        // #ifdef APP-PLUS
        terminal: 'app'
        // #endif
      }).then((res) => {
        if (res.code === 200) {
          if (!res.data) return
          setTimeout(() => {
            this.$store.commit('SET_MSGINFO', {
              show: true,
              content: Base64.decode(res.data.content),
              title: res.data.title
            })
          }, 2000)
        }
      })
    },

    openSystemSettings() {
      // 以下方法来自5+范畴，如需深究，请自行查阅相关文档
      // https://ask.dcloud.net.cn/docs/
      if (uni.getSystemInfoSync().platform === 'ios') {
        var UIApplication = plus.ios.import('UIApplication')
        var application2 = UIApplication.sharedApplication()
        var NSURL2 = plus.ios.import('NSURL')
        var setting2 = NSURL2.URLWithString('App-prefs:root=General')
        application2.openURL(setting2)
        plus.ios.deleteObject(setting2)
        plus.ios.deleteObject(NSURL2)
        plus.ios.deleteObject(application2)
        const threadClass = plus.ios.importClass('NSThread')
        const mainThread = plus.ios.invoke(threadClass, 'mainThread')
        // plus.ios.invoke(mainThread, 'exit')
        //上面的不行就用下面的
        plus.ios
          .import('UIApplication')
          .sharedApplication()
          .performSelector('exit')
      } else {
        var Intent = plus.android.importClass('android.content.Intent')
        var Settings = plus.android.importClass('android.provider.Settings')
        var mainActivity = plus.android.runtimeMainActivity()
        var intent = new Intent(Settings.ACTION_SETTINGS)
        mainActivity.startActivity(intent)
        plus.runtime.quit()
      }
    }
  }
}
</script>
<style lang="scss">
/*每个页面公共css */
@import 'uview-ui/index.scss';

* {
  font-weight: 500;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
    Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}

page {
}

// 优化uView Nacbar组件样式
.navbar_optimize {
  .u-navbar-placeholder {
    background-color: $theme-color;
  }
}
</style>
